package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.UnionActivityTaskMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;

import java.util.ArrayList;
import java.util.List;

/**
 * 商会任务
 */
public class UnionActivityTaskListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.BeKick.getValue()){
            long kickUserId = memberList.get(0).getUserId();

            CrossUnionMember roleMember = CrossUnionMgr.getUnionMember(unionInfo.getUnionUid(), playerId);
            if(roleMember != null && roleMember.getServerId() == GameServer.getInstance().getServerId()){
                UnionActivityTaskMgr.quitUnion(kickUserId);
            }
        }

        if(syncType == eUnionSyncType.JoinUnion.getValue()){
            CrossUnionMember joinMember = memberList.get(0);
            if(joinMember.getServerId() == GameServer.getInstance().getServerId()){
                UnionActivityTaskMgr.joinUnion(joinMember.getUserId());
            }
        }

        if (syncType == eUnionSyncType.QuitUnion.getValue()) {
            CrossUnionMember joinMember = memberList.get(0);
            if(joinMember.getServerId() == GameServer.getInstance().getServerId()){
                UnionActivityTaskMgr.quitUnion(joinMember.getUserId());
            }
        }

        if(syncType == eUnionSyncType.DeleteUnion.getValue()){
            List<Long> syncUserIdList = new ArrayList<>();
            for (CrossUnionMember member : memberList) {
                if(member.getServerId() == GameServer.getInstance().getServerId()){
                    syncUserIdList.add(member.getUserId());
                }
            }
            for (long userId : syncUserIdList) {
                UnionActivityTaskMgr.quitUnion(userId);
            }
        }
    }
}
